package com.jing.weibo.service.user;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jing.system.comm.contants.DataCons;
import com.jing.system.model.PageCls;
import com.jing.weibo.mapper.user.UserInfoMapper;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.pojo.user.UserSign;

/**
 * 用户的Service
 * @author jing.yue
 * @version 2012/08/30 1.0.0
 */
@Component
public class UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private UserSignService userSignService;

	/**
	 * 根据用户名和用户类型查询用户对象
	 * @param username
	 * @return
	 * @throws Exception
	 */
	public UserInfo findByUserName(String username) throws Exception {
		UserInfo userInfo = null;
		List<UserInfo> list = userInfoMapper.findByUserName(username);
		if(list != null && list.size() > 0) {
			userInfo = list.get(0);
			userInfo.setGradename(DataCons.gradeMap.get(userInfo.getGradeid()).getRemark());
			userInfo.setFreezemptime(DataCons.gradeMap.get(userInfo.getGradeid()).getFreezemptime());

			//处理用户今天是否签过到
			UserSign userSign = new UserSign();
			userSign.setUsername(userInfo.getUsername());
			if(userSignService.findByUsernameTodaySign(userSign) != null) {
				userInfo.setIssign("true");
			} else {
				userInfo.setIssign("false");
			}
		}
		return userInfo;
	}

	/**
	 * 根据用户名和用户类型修改用户信息
	 * @param userInfo
	 * @throws Exception
	 */
	@Transactional
	public void updateUserInfo(UserInfo userInfo) throws Exception {
		userInfoMapper.updateUserInfo(userInfo);
	}


	/**
	 * 保存用户信息
	 * @param userInfo
	 * @throws Exception
	 */
	@Transactional
	public void saveUserInfo(UserInfo userInfo) throws Exception {
		userInfoMapper.saveUserInfo(userInfo);
	}

	/**
	 * 根据用户名和用户类型修改开启互粉状态
	 * @param userInfo
	 * @throws Exception
	 */
	public void updateUserInfoIsopenmp(UserInfo userInfo) throws Exception {
		userInfoMapper.updateUserInfoIsopenmp(userInfo);
	}

	/**
	 * 修改用户是否开启同城互粉
	 * @param userInfo
	 * @throws Exception
	 */
	public void updateUserInfoIsopencwmp(UserInfo userInfo) throws Exception {
		userInfoMapper.updateUserInfoIsopencwmp(userInfo);
	}

	/**
	 * 根据用户名和用户类型修改用户token过期时间
	 * @param userInfo
	 * @throws Exception
	 */
	public void updateUserInfoTokentime(UserInfo userInfo) throws Exception {
		userInfoMapper.updateUserInfoTokentime(userInfo);
	}

	/**
	 * 修改用户积分和妞币和等级信息
	 * @param userInfo
	 * @throws Exception
	 */
	public void updateUserInfoPointCurrency(UserInfo userInfo) throws Exception {
		userInfoMapper.updateUserInfoPointCurrency(userInfo);
	}

	/**
	 * 根据用户名修改发出邀请用户的邀请总数量加1
	 * @param username
	 * @throws Exception
	 */
	public void updateUserInfoInvitenum(String username) throws Exception {
		userInfoMapper.updateUserInfoInvitenum(username);
	}

	/**
	 * 分页获取用户邀请的列表
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findInviteFriendInfo(UserInfo userInfo) throws Exception {
		List<UserInfo> userInfos = userInfoMapper.findInviteFriendInfo(userInfo);
		Integer countSize = userInfoMapper.findInviteFriendInfoCount(userInfo);
		PageCls pageCls = PageCls.setPageCls(userInfo.getPage(), userInfo.getSize(), null, null, countSize, userInfos.size(), userInfos);
		return pageCls.getPageInfo();
	}

	/**
	 * 分页根据用户昵称模糊查询好友信息
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> findByNicknameUserInfo(UserInfo userInfo) throws Exception {
		List<UserInfo> userInfos = userInfoMapper.findByNicknameUserInfo(userInfo);
		Integer countSize = userInfoMapper.findByNicknameUserInfoCount(userInfo);
		PageCls pageCls = PageCls.setPageCls(userInfo.getPage(), userInfo.getSize(), null, null, countSize, userInfos.size(), userInfos);
		return pageCls.getPageInfo();
	}

	/**
	 * 获取邀请排行榜的列表[前几位]
	 * @param topNum
	 * @return
	 * @throws Exception
	 */
	public List<UserInfo> findTopInviteFriendInfo(Integer topNum) throws Exception {
		return userInfoMapper.findTopInviteFriendInfo(topNum);
	}

	/**
	 * 根据用户名和类型获取参加了互粉的用户和在粉丝中不存在的前100个用户
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	/*public List<UserInfo> findByJoinmpUserInfo(UserInfo userInfo) throws Exception {
		userInfo.setStringtime(DateUtil.getTodayYyyyMmDdMaxHhmmss());
		return userInfoMapper.findByJoinmpUserInfo(userInfo);
	}*/

	/**
	 * 根据泡吧分区获取下面的所有用户信息
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	/*public List<UserInfo> findByJoinMpPbTypeUserInfo(UserInfo userInfo) throws Exception {
		userInfo.setStringtime(DateUtil.getTodayYyyyMmDdMaxHhmmss());
		return userInfoMapper.findByJoinMpPbTypeUserInfo(userInfo);
	}*/

	/**
	 * 查看没有分区的用户
	 * @param userInfo
	 * @param freezeUsers
	 * @return
	 * @throws Exception
	 */
	/*public List<UserInfo> findByNotPbTypeUserInfo(UserInfo userInfo, List<String> freezeUsers) throws Exception {
		userInfo.setStringtime(DateUtil.getTodayYyyyMmDdMaxHhmmss());
		if(freezeUsers != null && freezeUsers.size() > 0) {
			userInfo.setUsernames(freezeUsers.toArray(new String[freezeUsers.size()]));
		}
		return userInfoMapper.findByNotPbTypeUserInfo(userInfo);
	}*/

	/**
	 * 根据标签查询用户可以互粉的信息
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	/*public List<UserInfo> findByTagMpUserInfo(UserInfo userInfo) throws Exception {
		userInfo.setStringtime(DateUtil.getTodayYyyyMmDdMaxHhmmss());
		return userInfoMapper.findByTagMpUserInfo(userInfo);
	}*/
}
